package com.xinlu.user.action;

import com.xinlu.common.game.bo.ConstantLstBo;
import com.xinlu.derek.common.BaseAction;
import com.xinlu.derek.common.BaseForm;
import com.xinlu.persistence.Users;
import com.xinlu.user.bo.UsersBo;
import com.xinlu.user.dao.MD5Util;
import com.xinlu.user.form.XgzlForm;

public class XgzlAction extends BaseAction {

	private UsersBo usersBo;

	public void setUsersBo(UsersBo usersBo) {
		this.usersBo = usersBo;
	}

	public void setFr(XgzlForm fr) {
		this.fr = fr;
	}

	@Override
	public String doExecute() throws Exception {
		return null;
	}

	@Override
	public void formInit() {

	}

	XgzlForm fr = new XgzlForm();

	@Override
	public BaseForm getModel() {
		return fr;
	}

	/**
	 * 修改用户信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String changeUserInfo() throws Exception {

		Users crtUser = (Users) getSession().getAttribute("userinfo");
		crtUser = this.usersBo.loadUser(crtUser.getUsername());

		if (!MD5Util.encodePassword(fr.getPwd()).toLowerCase().equals(crtUser.getUserpass().toLowerCase())) {
			throw new Exception("密码错误");
		}

		crtUser.setQq(fr.getQq());
		crtUser.setBankArea(fr.getSheng() + fr.getShi());
		crtUser.setBankBank(fr.getTkyh());
		crtUser.setBankName(fr.getZsxm());
		crtUser.setBankNum(fr.getZh());

		usersBo.update(crtUser);
		getSession().setAttribute("userinfo", crtUser);// 赋值给缓存

		return "operSuccess";
	}
	
	/**
	 * 锁定资料
	 * 
	 * @return
	 * @throws Exception
	 */
	public String lockUserInfo() throws Exception {

		Users crtUser = (Users) getSession().getAttribute("userinfo");

		if (!MD5Util.encodePassword(fr.getPwd()).toLowerCase().equals(crtUser.getUserpass().toLowerCase())) {
			throw new Exception("密码错误");
		}

		crtUser.setIslock(1);

		usersBo.update(crtUser);
		getSession().setAttribute("userinfo", crtUser);// 赋值给缓存

		return loadPage();
	}

	/**
	 * 加载资料
	 * 
	 * @return
	 */
	public String loadPage() {

		Users crtUser = (Users) getSession().getAttribute("userinfo");

		fr.setYhlb(ConstantLstBo.getyhlb());
		fr.setTkyh(crtUser.getBankBank());
		fr.setQq(crtUser.getQq());
		fr.setSheng("");
		fr.setShi("");
		fr.setYhm(crtUser.getUsername());
		fr.setZh(crtUser.getBankNum());
		fr.setZsxm(crtUser.getBankName());
		fr.setDq(crtUser.getBankArea());
		fr.setPwd("");
		fr.setIslock(crtUser.getIslock()+"");

		return "xgzl";
	}

}
